package com.gallery.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gallery.exceptions.LoginFailedException;
import com.gallery.mapper.ImagesMapper;
import com.gallery.mapper.UserMapper;
import com.gallery.service.IImagesService;
import com.gallery.service.ILoveImagesService;
import com.gallery.service.IUserService;
import dto.UserLoginDTO;
import entity.Images;
import entity.LoveImages;
import entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import vo.UserLoginVO;
import vo.UserVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @param
 * @return
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService{
    private final UserMapper userMapper;
    private final ILoveImagesService iLoveImagesService;
    private final IImagesService imagesService;
    private final ImagesMapper imagesMapper;
    @Override
    public UserLoginVO login(UserLoginDTO userLoginDTO) {
        //搜索用户是否存在
        User user = lambdaQuery()
                .eq(User::getUsername, userLoginDTO.getUsername())
                .eq(User::getPassword, userLoginDTO.getPassword())
                .one();
        log.info("获取到的员工:{}",user);
        //为空则抛出异常
        if(ObjectUtil.isEmpty(user)){
            throw new LoginFailedException("账号或密码错误!");
        }
        //生成jwt令牌
        StpUtil.login(user.getId());
        UserLoginVO userLoginVO=new UserLoginVO();
        BeanUtils.copyProperties(user,userLoginVO);
        userLoginVO.setToken(StpUtil.getTokenValue());
        return userLoginVO;
    }

    @Override
    public UserVO myself() {
        UserVO userVO = new UserVO();
        User user = userMapper.selectById(StpUtil.getLoginIdAsLong());
        BeanUtil.copyProperties(user,userVO);
        //查询喜欢类别
        List<LoveImages> loveImages = iLoveImagesService.lambdaQuery()
                .eq(LoveImages::getUserId, StpUtil.getLoginIdAsLong())
                .list();
        List<Images> imagesList = new ArrayList<Images>();
        loveImages.forEach(love -> {
            Long imagesId = love.getImagesId();
            Images images = imagesMapper.selectById(imagesId);
            images.setDayHot(null);//dayhot不显示数据
            imagesList.add(images);
        });
        userVO.setLoveImages(imagesList);
        //查询用户有的作品
        List<Images> list = imagesService.lambdaQuery()
                .eq(Images::getUserId, StpUtil.getLoginIdAsLong())
                .list();
        System.out.println("list = " + list);
        List<Images> images=new ArrayList<Images>();
        list.forEach(l ->{
            images.add(l);
        });
        userVO.setImages(images);
        return userVO;
    }
}
